package org.example;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class UpdateStu {
    public static void main(String[] args) {
        Connection conn=JDBCConn.getConnection();
        PreparedStatement stmt=null;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入性别：");
        String sex = sc.next();
        System.out.println("请输入电话号码：");
        String telephone = sc.next();
        System.out.println("请输入qq号码：");
        String qq = sc.next();
        System.out.println("请输入年级号：");
        int gradeid = sc.nextInt();
        System.out.println("请输入入学时间：");
        String s = sc.next();
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
        String enterdate;
        try {
            enterdate = sdf.format (sdf.parse (s));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        System.out.println("请输入你来自哪个城市：");
        String fromcity = sc.next();
        System.out.println("请输入出生日期：");
        String s1 = sc.next();
        System.out.println("请输入当前居住地址：");
        String address = sc.next();
        System.out.println("请输入密码：");
        String pass = sc.next();
        String birthday;
        try {
            birthday = sdf.format (sdf.parse (s));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        try {
            String sql1="insert into s_student(sname,sex,telephone,qq,gradeid,enterdate,fromcity,birthday,address,pass)values(?,?,?,?,?,?,?,?,?,?)";
            stmt = conn.prepareStatement(sql1);
            stmt.setString(1, name);
            stmt.setString(2, sex);
            stmt.setString(3, telephone);
            stmt.setString(4, qq);
            stmt.setInt(5, gradeid);
            stmt.setDate(6, Date.valueOf(enterdate));
            stmt.setString(7, fromcity);
            stmt.setDate(8, Date.valueOf(birthday));
            stmt.setString(9, address);
            stmt.setString(10, pass);
            System.out.println(sql1);
            int rs = stmt.executeUpdate();
            System.out.println("更新了："+rs+"行！");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCConn.close(null,stmt,conn);
        }
    }
}
